import React from 'react';
import { Alert } from 'react-native';
import { Scene, Actions } from 'react-native-router-flux';
import Form from './Form';
import members from '../models/members';
import customer from '../models/customer';
import navBarStyle from '../../../components/navBarStyle';

const pressHandle = values => {
  if (!values.customerFamilyMemberId) {
    members.create(values).then(data => {
      if (data) {
        Alert.alert('操作成功', '新增家庭成员', [
          { text: '继续添加' },
          { text: '返回', onPress: Actions.pop }
        ]);
      }
    });
  } else {
    members.update(values).then(data => {
      if (data) {
        Alert.alert('操作成功', '更新家庭成员', [
          { text: '返回', onPress: Actions.pop }
        ]);
      }
    });
  }
};
module.exports = [
  <Scene
    key="editFamilyMember"
    component={Form}
    path="edit/family/member/:id"
    title="更新家庭成员"
    hideTabBar
    onSubmit={pressHandle}
    members={members}
    customer={customer}
    onEnter={({ customerId, memberId }) => {
      members.fetchOne(customerId, memberId)
    }}
    {...navBarStyle}
  />,
  <Scene
    key="createFamilyMember"
    component={Form}
    path="create/family/member"
    title="新增家庭成员"
    hideTabBar
    onSubmit={pressHandle}
    onEnter={members.resetCurrentMember}
    members={members}
    customer={customer}
    {...navBarStyle}
  />
];
